<?php
/*
+ ---------------------------------------------------------------------- +
| e107Shop - An e-commerce plugin for the e107 content management system |
|                                                                        |
| ByOrder Europe 2007-2008                                               |
| http://e107shop.com                                                    |
| patrick@byorder.nl                                                     |
|                                                                        |
| Released under the terms and conditions of the LGPL                    |
|                                                                        |
| $Revision: 1.0                                                         |
| $Date: 2008/08/20                                                      |
| $Author: Bieleke                                                       |
+ ---------------------------------------------------------------------- +
*/

function e107shop_product_list_attributes($product) {
    
    global $e107shop_db_name, $sql, $ns;
    $display = e107shop_display_product_list_attributes();
    if($product){
        $query = "itemid=".$product."";
    
    
        if(!$sql->db_Select($e107shop_db_name['product_options'], "*", $query)) {
            $ns->tablerender("<center>".lan_admin_menu_options."</center>", 
            "<center>".lan_admin_prod_no_prod_error."</center>");
        } else {    
            
            $display .= "<center><form name='form1'><br />
                        <table class='fborder' width='95%'>
                            <tr>
                                <td class='forumheader'>
                                    ".lan_manage_products_attributes_list."
                                </td>
                            </tr>
                            <tr>
                                <td class='forumheader2'>
                                    ".lan_manage_products_attributes.": <select class=\"tbox\" name='p' id='p'>";
                                
            while($row=$sql->db_Fetch()){
                    $display.="<option value='".$row["ID"]."'>".$row["option_group"].": ".$row["optname"]."</option>";
            }
                
            
            $display.="</option>
            </select>&nbsp;&nbsp;";

            $display.="<input class=\"tbox\" type='button' name='a' value='".lan_global_edit."' OnClick=\"window.location='admin_manage_attributes.php?a=1&p='+document.form1.p.options[document.form1.p.selectedIndex].value\">&nbsp;&nbsp;";
            $display.="<input class=\"tbox\" type='button' name='d' value='".lan_global_delete."' OnClick=\"window.location='admin_manage_attributes.php?a=2&p='+document.form1.p.options[document.form1.p.selectedIndex].value\">";
            $display.="</form>";
            $display.="</td></tr></table>";
        }
    } else {
        $display .= "<center><form name='form1'><br />
                        <table class='fborder' width='95%'>
                            <tr>
                                <td class='forumheader'>
                                    ".lan_manage_products_attributes_list."
                                </td>
                            </tr>
                            <tr>
                                <td class='forumheader2'>
                                    ".lan_manage_products_attributes.": <select class='tbox' name='p' id='p'>";
        $sql->db_Select($e107shop_db_name['product_options'], "*", "");
        while($row=$sql->db_Fetch()) {
            $display.="<option value='".$row["ID"]."'>".$row["option_group"].": ".$row["optname"]."</option>";
        }

        $display.="</select>&nbsp;&nbsp;<input class=\"tbox\" type='button' name='a' value='".lan_global_edit."' OnClick=\"window.location='admin_manage_attributes.php?a=1&p='+document.form1.p.options[document.form1.p.selectedIndex].value\">&nbsp;&nbsp;";
        $display.="<input class=\"tbox\" type='button' name='d' value='".lan_global_delete."' OnClick=\"window.location='admin_manage_attributes.php?a=2&p='+document.form1.p.options[document.form1.p.selectedIndex].value\">";
        $display.="</form>";
        $display.="</td></tr></table>";
    }

    $ns->tablerender("<center>".lan_admin_menu_options."</center>", $display);
    
}

#####################################################
# e107shop_display_category_list()
#####################################################
# 
#    This function is used to display a dropdown
#    list of categories.
#
#####################################################
function e107shop_display_product_list_attributes() {
    global $e107shop_db_name, $sql, $ns;

    $sql->db_Select($e107shop_db_name['products'], "*", "");
    $display.="<center><form name='form2'><br />
                <table class='fborder' width='95%'>
                    <tr>
                        <td class='forumheader'>
                            ".lan_filter_products_title."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2'>
                            ".lan_filter_products.": <select class=\"tbox\" name='g' id='g'>";
                        
    
    while($row=$sql->db_Fetch())
    {    
        
        if($row["ID"]==$_GET["g"])
        {
            $display.="<option selected value='".$row["ID"]."'>".$row["product_name"]."</option>";
        }
        else
        {
            $display.="<option value='".$row["ID"]."'>".$row["product_name"]."</option>";
        }
    }
    
    
    $display.="</select>&nbsp;&nbsp;<input class=\"tbox\" type='button' name='a' value='"
    .lan_admin_go."' OnClick=\"window.location='admin_manage_attributes.php?g='
    +document.form2.g.options[document.form2.g.selectedIndex].value\">";

    $display.="</form>";
    $display.="</td></tr></table><br />";
    
    
    return $display;
}

#####################################################
# e107shop_manage_attributes($action, $product)
#####################################################
# 
#    This function displays the form for adding/editing
#    a product.
#
#####################################################
function e107shop_manage_attributes($action, $attribute) {
    global $e107shop_db_name, $sql, $ns;
    if($action==1) {
        //
        //edit the product
        //
        $sql->db_Select($e107shop_db_name['product_options'], "*", "ID=".$attribute);
        while($row=$sql->db_Fetch()) {
            //
            // Store the fields in variables so they can be
            // used in the form, without having to write out the
            // form twice.
            //
            $product = $row["itemid"];
            $option_name=$row["optname"];
            $option_group=$row["option_group"];
            $option_value = $row["optval"];
            $operator = $row["opter"];    
            
        }
        $formaction = 1;
    } else {
        $formaction = 2;
    }

    //
    // Display form
    //

    $display = "<center><form action='".e_SELF."' method='POST' name='productform' ID='productform'><br />
                <table width='95%' class='fborder'>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_prod_at_group.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='option_group' value='".$option_group."'>
                            &nbsp;".lan_admin_prod_at_group_help."
                        </td>
                    </tr>
                    <tr>    
                        <td class='forumheader2' width='20%'>
                            ".lan_attribute_name.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='option_name' value='".$option_name."'>
                            &nbsp;".lan_attribute_name_help."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_prod_at_val.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='option_value' value='".$option_value."'>
                            &nbsp;".lan_admin_prod_at_val_help."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_prod_at_operator.": 
                        </td>
                        <td class='forumheader3'>
                            <select class=\"tbox\" type='text' name='operator'>";
    if($operator=="+"){
    $display.="<option selected value='+'>+</option>
                <option value='-'>-</option>";
    } else {
        $display.="<option selected value='-'>-</option>
                    <option value='+'>+</option>";
    }
    $display.="</select>
                &nbsp;".lan_admin_prod_at_operator_help."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_prod_at.": 
                        </td>
                        <td class='forumheader3'>
                            ".e107shop_get_product_select($product)."
                        </td>
                    </tr>                    
                    <tr>
                        <td class='forumheader' colspan='2'>
                            <input class=\"tbox\" type='hidden' value='".$formaction."' name='faction'>
                            <input class=\"tbox\" type='hidden' value='".$attribute."' name='attributeid'>
                            <center><input class=\"tbox\" type='submit' value='".lan_global_submit."' name='subbed'></center>
                        </td>
                    </tr>
                
                    </form>
                    </table>";

    if($action==1) {
        $ns->tablerender("<center>".lan_admin_prod_at_edit."</center>", $display);
    } else {
        $ns->tablerender("<center>".lan_admin_prod_at_new."</center>", $display);
    }
}

#####################################################
# e107shop_process_attributes($vars)
#####################################################
# 
#    This function processes the product and adds
#    delets or updates it in the database.
#
#####################################################

function e107shop_process_attribute($action, $option_name, $option_value, 
$option_group, $operat, $product, $attribute) {
    
    global $e107shop_db_name, $sql, $ns;
    if($action==1) {
        //
        // edit
        //
        $sql->db_Update($e107shop_db_name['product_options'], "optname='".$option_name."', optval='".
        $option_value."', option_group='".$option_group."', opter='".
        $operat."', itemid='".$product."' where ID=".$attribute);
        
        $ns->tablerender("<center>".lan_global_status."</center>", 
        "<center>".lan_global_updated."</center>");
    } else {
        //
        // Insert a new product
        //
        if ($option_group != '') $sql->db_Insert($e107shop_db_name['product_options'], "0, '".$product."', '"
        .$option_name."', '".$option_value."', '".$operat."', '".$option_group."'");
    }
}

function e107shop_get_product_select($product) {
    global $e107shop_db_name, $sql;
    $sql->db_Select($e107shop_db_name['products'], "*", "");
    $display ="<select name='product' class='tbox'>";
    while($row=$sql->db_Fetch())
    {
        if($row["ID"]==$product)
        {
            $display.="<option selected value='".$row["ID"]."'>".$row["product_name"]."</option>";
        }
        else
        {
            $display.="<option value='".$row["ID"]."'>".$row["product_name"]."</option>";
        }
    }
    return $display;
}

